/**
 * author：lemon + 10050
 * createtime ： 2013-2013-8-27 下午5:27:39
 * 业务异常，通用业务逻辑异常
 */
package com.gitee.xmhzzz.component.common.exception;


import com.gitee.xmhzzz.component.common.enums.ErrorCode;
import com.gitee.xmhzzz.component.common.enums.ResponseCode;

import java.util.Objects;

/**
  * @ClassName
  * @Description
  * @Author wzq
  * @Date 2023/12/15 17:05
  * @Version 1.0
  */
public class DefaultBusinessException extends AbstractBusinessException
{
    public DefaultBusinessException(String msg)
    {
        super(msg);
    }
    
    public DefaultBusinessException(String msg, Integer responseCode)
    {
        super(msg, responseCode);
    }
    

    public DefaultBusinessException(String msg, Exception e)
    {
        super(msg, e);
    }
    
    public DefaultBusinessException(String msg, Object... objs)
    {
        super(msg, objs);
    }

    private static final long serialVersionUID = 1L;
    

    @Override
    public int getResponseCode()
    {
        return super.getResponseCode();
    }

    protected static String format(ErrorCode code, Object... args) {
        // TODO:提示信息国际化？
        if (Objects.isNull(args)) {
            return code.getMsg();
        }
        return String.format(code.getMsg(), args);
    }

    /**
     * 抛出业务异常
     *
     * @param code    异常码
     * @param message 提示信息
     */
    public static void of(Integer code, String message) {
        throw new DefaultBusinessException(message, new ErrorCode() {
            @Override
            public Integer getCode() {
                return code;
            }

            @Override
            public String getMsg() {
                return message;
            }
        });
    }

    /**
     * 抛出业务异常
     *
     * @param code    异常码
     * @param message 提示信息
     */
    public static void of(ErrorCode code, String message) {
        throw new DefaultBusinessException(message, code);
    }

    /**
     * 抛出业务异常
     *
     * @param code    异常
     *
     */
    public static void of(ErrorCode code) {
        throw new DefaultBusinessException(code.getMsg(), code);
    }

    /**
     * 抛出业务异常
     *
     * @param message 提示信息
     */
    public static void of(String message) {
        throw new DefaultBusinessException(message, ResponseCode.SYSTEM_BUSY);
    }

    /**
     * 抛出业务异常
     *
     * @param exception 异常
     */
    public static void of(DefaultBusinessException exception) {
        throw exception;
    }
    
}
